package com.space.user.repositoryimpl;

import com.space.user.converter.UserAccountConverter;
import com.space.user.database.dao.UserAccountDao;
import com.space.user.database.dataobject.UserAccountDO;
import com.space.user.domain.account.entity.UserAccount;
import com.space.user.domain.account.repository.UserAccountRepository;
import com.space.user.domain.account.valueobject.UserId;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author <a href="mailto:414563035@qq.com">Bedroom</a>
 * @date 2021/4/26 3:38 下午
 */
@Repository
public class UserAccountRepositoryImpl implements UserAccountRepository {
    @Resource
    UserAccountDao userAccountDao;

    @Autowired
    UserAccountConverter userAccountConverter;

    @Override
    public @NonNull UserAccount find(UserId userId) {
        return userAccountConverter.toEntity(userAccountDao.selectById(userId.getUserId()));
    }

    @Override
    public void save(UserAccount userAccount) {
        UserAccountDO userAccountDO = userAccountConverter.toDO(userAccount);
        if (Objects.isNull(userAccountDO.getCreateTime())) {
            userAccountDao.insert(userAccountDO);
        } else {
            userAccountDao.updateById(userAccountDO);
        }
    }
}
